package com.ai.nsa.utils.pool;

import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.io.Closeable;
import java.io.Serializable;

/**
 * @author Feng Chen 2017/6/7.
 */
public abstract class PoolBase<T> implements Closeable, Serializable {

    protected GenericObjectPool<T> internalPool;

    public PoolBase() {
    }

    public PoolBase(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
        this.initPool(poolConfig, factory);
    }

    protected T borrowResource() {
        try {
            return internalPool.borrowObject();
        } catch (Exception e) {
            throw new ConnectionException("Could not borrow a resource from the pool.", e);
        }
    }

    protected void returnResource(T resource) {
        if (null != resource) {
            try {
                this.internalPool.returnObject(resource);
            } catch (Exception e) {
                throw new ConnectionException("Could not return the resource to the pool.", e);
            }
        }
    }

    protected void invalidateResource(T resource) {
        if (null != resource) {
            try {
                this.internalPool.invalidateObject(resource);
            } catch (Exception e) {
                throw new ConnectionException("Could not invalidate the resource to the pool", e);
            }
        }
    }

    public int getNumActive() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getNumActive();
    }

    public int getNumIdle() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getNumIdle();
    }

    public int getNumWaiters() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getNumWaiters();
    }

    public long getMeanBorrowWaitTimeMillis() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getMeanBorrowWaitTimeMillis();
    }

    public long getMaxBorrowWaitTimeMillis() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getMaxBorrowWaitTimeMillis();
    }

    public boolean isClosed() {
        try {
            return this.internalPool.isClosed();
        } catch (Exception e) {
            throw new ConnectionException("Could not check closed from the pool", e);
        }
    }

    public void clear() {
        try {
            this.internalPool.clear();
        } catch (Exception e) {
            throw new ConnectionException("Could not clear the pool", e);
        }
    }

    protected void initPool(GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
        if (internalPool != null) {
            this.destory();
        }
        this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
    }

    protected void destory() {
        this.close();
    }

    public void close() {
        try {
            this.internalPool.close();
        } catch (Exception e) {
            throw new ConnectionException("Could not destory the pool.", e);
        }
    }

    protected void addObjects(final int count) {
        try {
            for (int i = 0; i < count; i++) {
                this.internalPool.addObject();
            }
        } catch (Exception e) {
            throw new ConnectionException("Error trying to add idle objects", e);
        }
    }

    // SECTION: INNER HELPER

    private boolean isInactived() {
        try {
            return this.internalPool == null && this.internalPool.isClosed();
        } catch (Exception e) {
            throw new ConnectionException("Could not check inactived from the pool", e);
        }
    }


}
